/*
 * @(#)DateUtils.java      0.1   2013-2-20
 *
 * Copyright (c) 2013 xQuant Co., Ltd. All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * xQuant Co., Ltd. ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use it
 * only in accordance with the terms of the license agreement you entered
 * into with xQuant.
 */
package com.xquant.trade.utils;

import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 日期Utils类（基本的日期操作）
 *
 * @author hujf
 * @version 0.1 2013-2-20
 */
public class DateUtils {
    private static Logger logger = LoggerFactory.getLogger(DateUtils.class);
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String DATE_FORMAT1 = "yyyyMMdd";
    public static final String DATE_TIME_FORMAT = "yyyy/MM/dd HH:mm:ss";
    public static final String TIME_FORMAT = "HH:mm:ss";
    public static final long millisecondsOfDay = 1000 * 60 * 60 * 24;
    public static final String DATE_TIME_FORMAT_OTHER = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_TIME_FORMAT_WITH_MILLIS = "yyyy-MM-dd HH:mm:ss SSS";
    public static final String DATE_TIME_FORMAT_ONLY_MILLIS = "HH:mm:ss SSS";

    //add by yu.you
    public static final FastDateFormat DATE_FORMATOR = FastDateFormat.getInstance(DATE_FORMAT);
    public static final FastDateFormat DATE_FORMATOR1 = FastDateFormat.getInstance(DATE_FORMAT1);
    public static final FastDateFormat DATETIME_FORMATOR = FastDateFormat.getInstance(DATE_TIME_FORMAT);
    public static final FastDateFormat TIME_FORMATOR = FastDateFormat.getInstance(TIME_FORMAT);
    public static final FastDateFormat DATETIME_FORMATOR_OTHER = FastDateFormat.getInstance(DATE_TIME_FORMAT_OTHER);
    public static final FastDateFormat DATETIME_FORMATOR__WITH_MILLIS = FastDateFormat.getInstance(DATE_TIME_FORMAT_WITH_MILLIS);
    public static final FastDateFormat DATETIME_FORMATOR__ONLY_MILLIS = FastDateFormat.getInstance(DATE_TIME_FORMAT_ONLY_MILLIS);
    //public static final DateTimeFormatter format = DateTimeFormat.forPattern(DATE_FORMAT);

    public final static String[] week = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
    public final static String[] weekFull = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
    public static SimpleDateFormat SDF_YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat SDF_YYYYMMDD = new SimpleDateFormat("yyyyMMdd");
    public static SimpleDateFormat SDF_YYYY_MM_DD_HH_MM_SS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat SDF_YYYYMMDDHHMMSS = new SimpleDateFormat("yyyyMMddHHmmss");
    public static SimpleDateFormat SDF_YYYY_MM_DD_HH_MM_SS_SSS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
    public static SimpleDateFormat SDF_YYYYMMDDHHMMSSSSS = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static String YYYYMMDD = "yyyyMMdd";
    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss SSS";
    public static String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
    public static final long ONE_DAY_MILLS = 3600000 * 24;
    public static final int WEEK_DAYS = 7;

    /**
     * 将2012-12-21 转为中文日期 2012年12月21日
     *
     * @param dateStr
     * @return
     */
    public static String formatChineseDate(Object dateStr) {
        if (null == dateStr) {
            return null;
        } else {
            return DateUtil.formatChineseDate(DateUtil.parseDate(dateStr.toString()), false, false);
        }
    }

    /**
     * 将传入的日期格式化成"yyyy-MM-dd"
     *
     * @param beginDate 需要做处理的日期
     * @return
     */
    public static String getFormatDate(String beginDate) {
        if (beginDate == null || StringUtils.isBlank(beginDate.trim())) {
            return null;
        }
        Date date = null;
        try {
            date = DATE_FORMATOR.parse(beginDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (date == null) {
            return null;
        }
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        return DATE_FORMATOR.format(cale.getTime());
    }

    /**
     * 根据传入的日期和需要加减的数字，得到处理后的日期
     *
     * @param beginDate 需要做处理的日期
     * @param t         需要加减的数字（需要往前推就写负数）
     * @return
     */
    public static String getCalculateDate(String beginDate, int t) {
        if (beginDate == null || StringUtils.isBlank(beginDate.trim())) {
            return null;
        }
        Date date = null;
        try {
            date = DATE_FORMATOR.parse(beginDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (date == null) {
            return null;
        }
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.add(Calendar.DATE, t); // 加减天数
        return DATE_FORMATOR.format(cale.getTime());
    }

    /**
     * 将格式为yyyy-MM-dd的日期字符串转换为Date类型。
     *
     * @param dateStr
     * @return
     */
    public static Date getDateByFormatString(String dateStr) {
        if (StringUtils.isBlank(dateStr)) {
            return null;
        }
        Date date = null;
        try {
            date = DATE_FORMATOR.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
        return date;
    }

    /**
     * 将格式为yyyy-MM-dd的日期字符串转换为Date类型。
     *
     * @param dateStr1
     * @return
     */
    public static Date getDateByFormatString1(String dateStr1) {
        if (StringUtils.isBlank(dateStr1)) {
            return null;
        }
        Date date = null;
        try {
            date = DATE_FORMATOR1.parse(dateStr1);
        } catch (ParseException e) {
            return null;
        }
        return date;
    }

    /**
     * 将Date类型转换为格式为yyyy-MM-dd的日期字符串。
     *
     * @param date
     * @return
     */
    public static String getFormatStringByDate(Date date) {
        if (date == null) {
            return null;
        }
        String dateStr = null;
        try {
            dateStr = DATE_FORMATOR.format(date);
        } catch (Exception e) {
            return null;
        }
        return dateStr;
    }

    /**
     * 将Date类型转换为格式为日期字符串。
     *
     * @param date
     * @return
     */
    public static String getFormatStringByDate(Date date, String formatStr) {
        if (date == null) {
            return null;
        }
        String dateStr = null;
        try {
            FastDateFormat formstor = FastDateFormat.getInstance(formatStr);
            dateStr = formstor.format(date);
        } catch (Exception e) {
            return null;
        }
        return dateStr;
    }


    public static Date getDateByFormatString(String dateStr, String formatStr) {
        if (dateStr == null || formatStr == null) {
            return null;
        }
        Date date = null;
        try {
            FastDateFormat formstor = FastDateFormat.getInstance(formatStr);
            date = formstor.parse(dateStr);
        } catch (Exception e) {
            return null;
        }
        return date;
    }

    /**
     * 获取当前日期，返回为yyyy-MM-dd格式的字符串。
     *
     * @return
     */
    public static String getCurrentDate() {
        Calendar cale = Calendar.getInstance();
        return DATE_FORMATOR.format(cale.getTime());
    }

    /**
     * 获取当前时间，返回为HH:mm:ss格式的字符串。
     *
     * @return
     */
    public static String getCurrentTime() {
        Calendar cale = Calendar.getInstance();
        return TIME_FORMATOR.format(cale.getTime());
    }

    /**
     * 判断传入的第一个日期是否早于第二个日期
     * <p>
     * 若第一个日期早于第二个日期，返回true，否则返回false
     * <p>
     * 例：isBeforeDate(2013-03-05, 2013-03-06) 为true isBeforeDate(2013-03-06,
     * 2013-03-06) 为false
     *
     * @param dateBefore 第一个日期
     * @param dateAfter  第二个日期
     * @return
     */
    public static boolean isBeforeDate(String dateBefore, String dateAfter) {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        try {
            cal1.setTime(DATE_FORMATOR.parse(dateBefore));
            cal2.setTime(DATE_FORMATOR.parse(dateAfter));
            if (cal1.before(cal2)) {
                return true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 计算两个日期的差值
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return 天数
     */
    public static int getDateBetweenDiffer(String endDate, String startDate) {
        Date sdate = null;
        Date edate = null;
        try {
            sdate = DATE_FORMATOR.parse(startDate);
            edate = DATE_FORMATOR.parse(endDate);
        } catch (ParseException e) {
            e.getStackTrace();
        }
        return (int) ((edate.getTime() - sdate.getTime()) / millisecondsOfDay);
    }

    /**
     * 获取当前日期时间，返回为yyyy/MM/dd HH:mm:ss格式的字符串。
     *
     * @return
     */
    public static String getCurrentDateTime() {
        Calendar cale = Calendar.getInstance();
        return DATETIME_FORMATOR.format(cale.getTime());
    }

    /**
     * 获取当前日期时间，返回为yyyy-MM-dd HH:mm:ss格式的字符串。
     *
     * @return
     */
    public static String getCurrentDateTimeOther() {
        Calendar cale = Calendar.getInstance();
        return DATETIME_FORMATOR_OTHER.format(cale.getTime());
    }

    /**
     * 获取当前日期时间，返回为yyyy-MM-dd HH:mm:ss SSS格式的字符串。
     *
     * @return
     */
    public static String getCurrentDateTimeWithMilliS() {
        Calendar cale = Calendar.getInstance();
        return DATETIME_FORMATOR__WITH_MILLIS.format(cale.getTime());
    }

    /**
     * 获取当前日期时间，返回为HH:mm:ss SSS格式的字符串。
     *
     * @return
     */
    public static String getCurrentDateTimeWOnlyMilliS() {
        Calendar cale = Calendar.getInstance();
        return DATETIME_FORMATOR__ONLY_MILLIS.format(cale.getTime());
    }

    /**
     * 根据日期信息，获取日期所在月份信息
     *
     * @return
     * @throws ParseException
     */
    public static int getMonthByDate(String date) throws ParseException {
        Calendar cale = Calendar.getInstance();
        cale.setTime(DATE_FORMATOR.parse(date));
        return cale.get(Calendar.MONTH) + 1;
    }

    /**
     * 根据日期信息，获取日期所在年份信息
     *
     * @return
     * @throws ParseException
     */
    public static int getYearByDate(String date) throws ParseException {
        Calendar cale = Calendar.getInstance();
        cale.setTime(DATE_FORMATOR.parse(date));
        return cale.get(Calendar.YEAR);
    }

    public static String getYearMonthByDate(String date) {
        Calendar cale = Calendar.getInstance();
        try {
            cale.setTime(DATE_FORMATOR.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int month = cale.get(Calendar.MONTH) + 1;
        String monthStr = "";
        if (month < 10) {
            monthStr = "0" + month;
        } else {
            monthStr = month + "";
        }
        return cale.get(Calendar.YEAR) + "" + monthStr;
    }

    /**
     * 根据日期信息，获取日期所在日期信息
     *
     * @return
     * @throws ParseException
     */
    public static int getDayByDate(String date) throws ParseException {
        Calendar cale = Calendar.getInstance();
        cale.setTime(DATE_FORMATOR.parse(date));
        return cale.get(Calendar.DATE);
    }

    /**
     * 获取某年某月第一天的日期
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static String getFirstDateOfMonth(String date) throws ParseException {
        Date dt = DATE_FORMATOR.parse(date);
        Calendar cale = Calendar.getInstance();
        cale.setTime(dt);
        int value = cale.getActualMinimum(Calendar.DAY_OF_MONTH);
        cale.set(Calendar.DAY_OF_MONTH, value);
        return DATE_FORMATOR.format(cale.getTime());
    }

    /**
     * 获取某年某月最后一天的日期
     *
     * @param date
     * @return
     */
    public static String getLastDateOfMonth(String date) throws ParseException {
        Date dt = DATE_FORMATOR.parse(date);
        Calendar cale = Calendar.getInstance();
        cale.setTime(dt);
        int value = cale.getActualMaximum(Calendar.DAY_OF_MONTH);
        cale.set(Calendar.DAY_OF_MONTH, value);
        return DATE_FORMATOR.format(cale.getTime());
    }

    public static String renameDayUnit(String days) {
        if (StringUtils.isBlank(days)) {
            return "";
        }
        return days.replace("D", "日").replace("W", "周").replace("M", "月").replace("Y", "年");
    }

    /**
     * 判断传入的日期是否是工作日
     *
     * @param date 需判断的日期
     * @return true/false
     */
    public static boolean isWorkDate(String date) {
        Date sdate;
        try {
            sdate = DATE_FORMATOR.parse(date);
            Calendar cale = Calendar.getInstance();
            cale.setTime(sdate);
            if (cale.get(Calendar.DAY_OF_WEEK) == 1 || cale.get(Calendar.DAY_OF_WEEK) == 7) {
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 判断传入月份的最后一天<br/>
     * <p>
     * 如传入"2012-02"，返回"2012-02-29"
     *
     * @param month 需判断的日期, 格式必须为"2013-05"
     */
    public static String getLastDayOfMonth(String month) {
        String[] date = month.split("-");
        if (date.length < 2) {
            return null;
        }
        if ("01".equals(date[1]) || "03".equals(date[1]) || "05".equals(date[1]) || "07".equals(date[1]) || "08".equals(date[1]) || "10".equals(date[1]) || "12".equals(date[1])) {
            return month + "-31";
        }
        if ("04".equals(date[1]) || "06".equals(date[1]) || "09".equals(date[1]) || "11".equals(date[1])) {
            return month + "-30";
        }
        if ("02".equals(date[1])) {
            int year = Integer.parseInt(date[0]);
            if (year % 400 == 0) {
                return month + "-29";
            }
            if ((year % 4 == 0) && (year % 100 > 0)) {
                return month + "-29";
            }
            return month + "-28";
        }
        return null;
    }


    public static boolean between(String dateString, String startDateString, String endDateString) {
        return between(dateString, startDateString, endDateString, DATE_FORMAT);
    }


    public static boolean between(String dateString, String startDateString, String endDateString,
                                  String formatString) {
        if (StringUtils.isEmpty(startDateString)
                || StringUtils.isEmpty(endDateString)) {
            return false;
        }
        if (StringUtils.isEmpty(formatString)) {
            formatString = DATE_FORMAT;
        }

        try {
            DateFormat df = new SimpleDateFormat(formatString);
            Date currentDate = df.parse(dateString);
            Date startDate = df.parse(startDateString);
            Date endDate = df.parse(endDateString);
            if (currentDate.getTime() >= startDate.getTime()
                    && currentDate.getTime() <= endDate.getTime()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }


    /**
     * 两个日期的相差月数
     *
     * @param startDate yyyy-MM-dd
     * @param endDate   yyyy-MM-dd
     * @return
     */
    public static double calculateMonthIn(String startDate, String endDate) {
        Calendar cal1 = new GregorianCalendar();
        cal1.setTime(DateUtils.getDateByFormatString(startDate));
        Calendar cal2 = new GregorianCalendar();
        cal2.setTime(DateUtils.getDateByFormatString(endDate));
        double c = (cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR)) * 12 +
                cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH) +
                (cal1.get(Calendar.DAY_OF_MONTH) - cal2.get(Calendar.DAY_OF_MONTH)) * 0.01;
        return c;
    }

    /**
     * 日期天数增加，返回日期格式字符串
     *
     * @param dateString String
     * @param days
     * @return String
     */
    public static String addDate(String dateString, Integer days) {
        try {
            DateFormat df = new SimpleDateFormat(DATE_FORMAT);
            Calendar cal = new GregorianCalendar();
            Date d = df.parse(dateString);
            cal.setTime(d);
            cal.add(Calendar.DATE, days);
            return df.format(cal.getTime());
        } catch (ParseException e) {
            return dateString;
        }
    }


    public static boolean isSeasonEndDate(String baseDate) throws ParseException {
        if (StringUtils.isBlank(baseDate)) {
            return false;
        }
        Calendar calender = Calendar.getInstance();
        calender.setTime(DateUtils.getDateByFormatString(baseDate));

        int month = calender.get(Calendar.MONTH) + 1;
        int day = calender.get(Calendar.DATE);
        int maxDay = calender.getActualMaximum(Calendar.DAY_OF_MONTH);
        if (month % 3 == 0 && maxDay == day) {
            return true;
        }
        return false;
    }

    /**
     * 日期月份增加，返回日期格式字符串
     *
     * @param dateString
     * @param months
     * @return String
     */
    public static String addMonth(String dateString, Integer months) {
        try {
            DateFormat df = new SimpleDateFormat(DATE_FORMAT);
            Calendar cal = new GregorianCalendar();
            Date d = df.parse(dateString);
            cal.setTime(d);
            cal.add(Calendar.MONTH, months);
            return df.format(cal.getTime());
        } catch (ParseException e) {
            return dateString;
        }
    }

    /**
     * 加上期限
     *
     * @param dateString
     * @param term       D,W,M,Y
     * @return
     */
    public static String addTerm(String dateString, String term) {
        String unit = term.substring(term.length() - 1);
        Integer val = Integer.parseInt(term.replace(unit, ""));
        if ("D".equals(unit)) {
            return addDate(dateString, val);
        } else if ("W".equals(unit)) {
            return addDate(dateString, val * 7);
        } else if ("M".equals(unit)) {
            return addMonth(dateString, val);
        } else if ("Y".equals(unit)) {
            return addYear(dateString, val);
        }
        return null;
    }

    /**
     * 日期月份增加，返回日期格式字符串
     *
     * @param dateString
     * @param years
     * @return String
     */
    public static String addYear(String dateString, Integer years) {
        try {
            DateFormat df = new SimpleDateFormat(DATE_FORMAT);
            Calendar cal = new GregorianCalendar();
            Date d = df.parse(dateString);
            cal.setTime(d);
            cal.add(Calendar.YEAR, years);
            return df.format(cal.getTime());
        } catch (ParseException e) {
            return dateString;
        }
    }

    /**
     * 时间格式转化 date 转为 String
     *
     * @param date
     * @return
     */
    public static String getDateFormat(Date date) {
        return DATETIME_FORMATOR_OTHER.format(date);
    }

    /**
     * 日期转换为指定格式字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static String date2Str(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 日期转换为默认格式字符串YYYY_MM_DD（yyyy-MM-dd）
     *
     * @param date
     * @return
     */
    public static String date2Str_YYYY_MM_DD(Date date) {
        return SDF_YYYY_MM_DD.format(date);
    }

    /**
     * 日期转换为默认格式字符串YYYYMMDD（yyyyMMdd）
     *
     * @param date
     * @return
     */
    public static String date2Str_YYYYMMDD(Date date) {
        return SDF_YYYYMMDD.format(date);
    }

    /**
     * 日期转换为默认格式字符串YYYY_MM_DD_HH_MM_SS（yyyy-MM-dd HH:mm:ss）
     *
     * @param date
     * @return
     */
    public static String date2Str_YYYY_MM_DD_HH_MM_SS(Date date) {
        return SDF_YYYY_MM_DD_HH_MM_SS.format(date);
    }

    /**
     * 日期转换为默认格式字符串YYYYMMDDHHMMSS（yyyyMMddHHmmss）
     *
     * @param date
     * @return
     */
    public static String date2Str_YYYYMMDDHHMMSS(Date date) {
        return SDF_YYYYMMDDHHMMSS.format(date);
    }

    /**
     * 日期转换为默认格式字符串YYYYMMDDHHMMSSSSS（yyyyMMddHHmmssSSS）
     *
     * @param date
     * @return
     */
    public static String date2Str_YYYYMMDDHHMMSSSSS(Date date) {
        return SDF_YYYYMMDDHHMMSSSSS.format(date);
    }

    /**
     * 日期转换为默认格式字符串YYYYMMDDHHMMSSSSS（yyyyMMddHHmmssSSS）
     *
     * @param date
     * @return
     */
    public static String date2Str_YYYY_MM_DD_HH_MM_SS_SSS(Date date) {
        return SDF_YYYY_MM_DD_HH_MM_SS_SSS.format(date);
    }

    /**
     * 字符串转换为指定格式日期
     * (注意：当你输入的日期是2014-12-21 12:12，format对应的应为yyyy-MM-dd HH:mm 否则异常抛出)
     *
     * @param date
     * @param format
     * @return
     * @throws ParseException
     * @
     */
    public static Date str2Date(String date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(date);
        } catch (ParseException ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex.toString());
        }
    }

    /**
     * 字符串转换为默认格式日期YYYYMMDD
     *
     * @param date
     * @return
     * @throws ParseException
     * @
     */
    public static Date str2Date_YYYYMMDD(String date) {
        try {
            return SDF_YYYYMMDD.parse(date);
        } catch (ParseException ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex.toString());
        }
    }

    /**
     * 字符串转换为默认格式日期YYYY_MM_DD
     *
     * @param date
     * @return
     * @throws ParseException
     * @
     */
    public static Date str2Date_YYYY_MM_DD(String date) {
        try {
            return SDF_YYYY_MM_DD.parse(date);
        } catch (ParseException ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex.toString());
        }
    }

    /**
     * 字符串转换为默认格式日期YYYY_MM_DD_HH_MM_SS
     *
     * @param date
     * @return
     * @throws ParseException
     * @
     */
    public static Date str2Date_YYYY_MM_DD_HH_MM_SS(String date) {
        try {
            return SDF_YYYY_MM_DD_HH_MM_SS.parse(date);
        } catch (ParseException ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex.toString());
        }
    }

    /**
     * 字符串转换为默认格式日期YYYYMMDDHHMMSS
     *
     * @param date
     * @return
     * @throws ParseException
     * @
     */
    public static Date str2Date_YYYYMMDDHHMMSS(String date) {
        try {
            return SDF_YYYYMMDDHHMMSS.parse(date);
        } catch (ParseException ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex.toString());
        }
    }

    /**
     * 字符串转换为默认格式日期YYYY_MM_DD_HH_MM_SS_SSS
     *
     * @param date
     * @return
     * @throws ParseException
     * @
     */
    public static Date str2Date_YYYY_MM_DD_HH_MM_SS_SSS(String date) {
        try {
            return SDF_YYYY_MM_DD_HH_MM_SS_SSS.parse(date);
        } catch (ParseException ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex.toString());
        }
    }

    /**
     * 字符串转换为默认格式日期YYYYMMDDHHMMSSSSS
     *
     * @param date
     * @return
     * @throws ParseException
     * @
     */
    public static Date str2Date_YYYYMMDDHHMMSSSSS(String date) {
        try {
            return SDF_YYYYMMDDHHMMSSSSS.parse(date);
        } catch (ParseException ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex.toString());
        }
    }

    /**
     * 描述：获取指定日期的中文星期数
     *
     * @param date 指定日期
     * @return 星期数，如：星期一
     */
    public static String getWeekStr(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int week = calendar.get(7);
        --week;
        String weekStr = weekFull[week];
        return weekStr;
    }

    /**
     * 描述：间隔时间
     *
     * @param date1
     * @param date2
     * @return 毫秒数
     */
    public static long getDateMiliDispersion(Date date1, Date date2) {
        if ((date1 == null) || (date2 == null)) {
            return 0L;
        }
        long time1 = date1.getTime();
        long time2 = date2.getTime();
        return time1 - time2;
    }

    /**
     * 描述：间隔天数
     *
     * @param date1
     * @param date2
     * @return 天数
     */
    public static int getDateInterval(Date date1, Date date2) {
        if ((date1 == null) || (date2 == null)) {
            return 0;
        }
        long time1 = date1.getTime();
        long time2 = date2.getTime();
        long interval = time1 - time2;
        Long longValue = new Long(interval / ONE_DAY_MILLS);
        return longValue.intValue();
    }

    /**
     * 描述：获取指定日期 增加或者减少天数
     *
     * @param date 指定日期
     * @param day  天数
     * @return 日期
     */
    public static Date getDateInterval(Date date, int day) {
        if (date == null) {
            return null;
        }
        long time = date.getTime();
        time += ONE_DAY_MILLS * day;
        return new Date(time);
    }

    /**
     * 获取日期时间当月的总天数，如2017-02-13，返回28
     *
     * @param date
     * @return
     */
    public static int getDaysOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.getActualMaximum(Calendar.DATE);
    }

    /**
     * 根据时间获取当月最大的日期
     * <li>2017-02-13，返回2017-02-28</li>
     * <li>2016-02-13，返回2016-02-29</li>
     * <li>2016-01-11，返回2016-01-31</li>
     *
     * @param date Date
     * @return
     * @throws Exception
     */
    public static Date getMaxDateOfMonth(Date date) throws Exception {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int value = cal.getActualMaximum(Calendar.DATE);
        return str2Date_YYYY_MM_DD(date2Str(date, "yyyy-MM") + "-" + value);
    }

    /**
     * 时间加减小时
     *
     * @param startDate 要处理的时间，Null则为当前时间
     * @param hours     加减的小时
     * @return Date
     */
    public static Date dateAddHours(Date startDate, int hours) {
        if (startDate == null) {
            startDate = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        c.set(Calendar.HOUR, c.get(Calendar.HOUR) + hours);
        return c.getTime();
    }

    /**
     * 时间加减秒数
     *
     * @param startDate 要处理的时间，Null则为当前时间
     * @param startDate   加减的秒数
     * @return
     */
    public static Date dateAddSeconds(Date startDate, int seconds) {
        if (startDate == null) {
            startDate = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        c.set(Calendar.SECOND, c.get(Calendar.SECOND) + seconds);
        return c.getTime();
    }

    /**
     * 时间比较（如果myDate>compareDate返回1，<返回-1，相等返回0）
     *
     * @param myDate      时间
     * @param compareDate 要比较的时间
     * @return int
     */
    public static int dateCompare(Date myDate, Date compareDate) {
        Calendar myCal = Calendar.getInstance();
        Calendar compareCal = Calendar.getInstance();
        myCal.setTime(myDate);
        compareCal.setTime(compareDate);
        return myCal.compareTo(compareCal);
    }

    /**
     * 获取两个时间中最小的一个时间
     *
     * @param date
     * @param compareDate
     * @return
     */
    public static Date dateMin(Date date, Date compareDate) {
        if (date == null) {
            return compareDate;
        }
        if (compareDate == null) {
            return date;
        }
        if (1 == dateCompare(date, compareDate)) {
            return compareDate;
        } else if (-1 == dateCompare(date, compareDate)) {
            return date;
        }
        return date;
    }

    /**
     * 获取两个时间中最大的一个时间
     *
     * @param date
     * @param compareDate
     * @return
     */
    public static Date dateMax(Date date, Date compareDate) {
        if (date == null) {
            return compareDate;
        }
        if (compareDate == null) {
            return date;
        }
        if (1 == dateCompare(date, compareDate)) {
            return date;
        } else if (-1 == dateCompare(date, compareDate)) {
            return compareDate;
        }
        return date;
    }

    /**
     * 时间加减分钟
     *
     * @param startDate 要处理的时间，Null则为当前时间
     * @param minutes   加减的分钟
     * @return
     */
    public static Date dateAddMinutes(Date startDate, int minutes) {
        if (startDate == null) {
            startDate = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + minutes);
        return c.getTime();
    }


    /**
     * 获取日期时间当年的总天数，如2017-02-13，返回2017年的总天数
     *
     * @param date
     * @return
     */
    public static int getDaysOfYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.getActualMaximum(Calendar.DAY_OF_YEAR);
    }

    /**
     * 描述：获取本年
     *
     * @return
     */
    public static int getCurrentYear() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(1);
    }

    /**
     * 描述：获取本月
     *
     * @return
     */
    public static int getCurrentMonth() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(2) + 1;
    }

    /**
     * 描述：获取本月的当前日期数
     *
     * @return
     */
    public static int getCurrentDay() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(5);
    }

    /**
     * 描述：获取当前日期 格式YYYYDDSS
     *
     * @return String
     */
    public static String getCurr_YYYYDDSS() {
        String str = date2Str_YYYYMMDD(new Date());
        return str;
    }

    /**
     * 描述：获取当前日期 格式YYYY_MM_DD
     *
     * @return String
     */
    public static String getCurr_YYYY_MM_DD() {
        String str = date2Str_YYYY_MM_DD(new Date());
        return str;
    }

    /**
     * 描述：获取当前日期 格式YYYY_MM_DD_HH_MM_SS
     *
     * @return String
     */
    public static String getCurr_YYYY_MM_DD_HH_MM_SS() {
        String str = date2Str_YYYY_MM_DD_HH_MM_SS(new Date());
        return str;
    }

    /**
     * 描述：获取当前日期 格式YYYYMMDDHHMMSS
     *
     * @return String
     */
    public static String getCurr_YYYYMMDDHHMMSS() {
        String str = date2Str_YYYYMMDDHHMMSS(new Date());
        return str;
    }

    /**
     * 描述：获取当前日期 格式YYYYMMDDHHMMSSSSS
     *
     * @return String
     */
    public static String getCurr_YYYYMMDDHHMMSSSSS() {
        String str = date2Str_YYYYMMDDHHMMSSSSS(new Date());
        return str;
    }

    /**
     * 描述：获取当前日期 格式YYYY_MM_DD_HH_MM_SS_SSS
     *
     * @return String
     */
    public static String getCurr_YYYY_MM_DD_HH_MM_SS_SSS() {
        String str = date2Str_YYYY_MM_DD_HH_MM_SS_SSS(new Date());
        return str;
    }

    /**
     * 描述:获取日期区间的的日期列表
     */
    public static List<String> getRangeDate(String begDate, String endDate) throws ParseException {
        List<String> days = new ArrayList<String>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date start = dateFormat.parse(begDate);
        Date end = dateFormat.parse(endDate);
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);

        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
        while (tempStart.before(tempEnd)) {
            days.add(dateFormat.format(tempStart.getTime()));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return days;
    }
}
